package cn.enilu.flash.api.controller.business;

import cn.enilu.flash.api.controller.BaseController;
import cn.enilu.flash.bean.vo.business.City;
import cn.enilu.flash.bean.vo.business.CityInfo;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.dao.MongoRepository;
import cn.enilu.flash.service.front.PositionService;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created  on 2017/12/29 0029.
 *
 * @Author enilu
 */
@RestController
public class PositionController extends BaseController {

    @Autowired
    private MongoRepository mongoRepository;
    @Autowired
    private PositionService positionService;

    /**
     * 处理城市信息请求的控制器方法
     * 根据不同的type参数返回不同的城市数据
     *
     * @param type    请求类型，支持"guess"、"hot"、"group"三种
     * @param request HTTP请求对象，用于获取客户端IP等信息
     * @return 返回处理结果，包含成功或失败状态及相应数据
     */
    @RequestMapping(value = "/v1/cities", method = RequestMethod.GET)
    public Object cities(@RequestParam("type") String type, HttpServletRequest request) {
        // 从MongoDB中获取城市数据
        Map cities = mongoRepository.findOne("cities");
        // 提取城市数据中的具体内容
        Map data = (Map) cities.get("data");
        // 根据不同的type类型进行数据处理
        switch (type) {
            case "guess":
                // 根据IP获取位置信息
                CityInfo cityInfo = positionService.getPostion(getIp());
                if (cityInfo != null) {
                    // 获取城市名称
                    String city = cityInfo.getCity();
                    // 如果城市名称为空则返回失败
                    if (Strings.isNullOrEmpty(city)) {
                        return Rets.failure();
                    }
                    // 返回根据城市名称查询的结果
                    return Rets.success(positionService.findByName(city));
                } else {
                    // 如果位置信息获取失败则返回成功但无数据
                    Rets.success();
                }

            case "hot":

                // 返回热门城市数据
                return Rets.success(data.get("hotCities"));

            case "group":
                // 返回分组城市数据
                return Rets.success(data);


            default:
                // 不支持的type类型，跳出switch语句
                break;


        }
        // 默认返回失败
        return Rets.failure();

    }

    @RequestMapping(value = "/v1/cities/{id}", method = RequestMethod.GET)
    public Object getCity(@PathVariable("id") Integer id) {
        return Rets.success(positionService.findById(id));
    }

    @RequestMapping(value = "/v1/pois", method = RequestMethod.GET)
    public Object getPoiByCityAndKeyword(@RequestParam(value = "type", defaultValue = "search") String type,
                                         @RequestParam(value = "city_id", required = false) Integer cityId,
                                         @RequestParam(value = "keyword") String keyword) {
        String cityName = null;
        if (cityId == null) {
            City city = positionService.guessCity(getIp());
            cityName = city.getName();
        } else {
            Map map = positionService.findById(cityId);
            cityName = map.get("name").toString();
        }
        return Rets.success(positionService.searchPlace(cityName, keyword));

    }

    @RequestMapping(value = "/v1/position/pois", method = RequestMethod.GET)
    public Object getPoiByGeoHash(@RequestParam("geohash") String geoHash) {
        System.out.println("geohash:" + geoHash);
        return Rets.success(positionService.pois(geoHash));
    }
}
